<?php

namespace App\Http\Controllers\Admin;

use App\Http\Models\Course;
use App\Http\Models\Profession;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Response;

class CourseController extends Controller
{
    public function lst()
    {
        $size = 2;
        $courseData = Course::paginate($size);
        // 获取课程的专业信息
        $courseData->each( function($course, $index){
            $course->profession = Profession::find($course->pro_id); // 查询专业信息，赋值给课程的 profession属性（自定义的属性）
        });

        return view('admin.course.lst', compact('courseData'));

    }

    public function add(Request $request)
    {
        if($request->isMethod('POST')){
            $data = $request->except(['_token']);
            $status = Course::create($data);
            if($status){
                return Response::json([
                    'status' => 'success',
                    'code' => 1000,
                    'msg' => '课程添加成功',
                    'data' => $status,
                ]);
            }else{
                return Response::json([
                    'status' => 'failure',
                    'code' => 1002,
                    'msg' => '课程添加失败',
                ]);
            }
        }
        // 专业
        $professionData = Profession::all();
        return view('admin.course.add', compact('professionData'));

    }
}
